#==============================================================================#
# Script by: Tigurus Fay                                                       #
#==============================================================================#
# Script: Morale System Add-on                                                 #
# Version: v1.2                                                                #
# Install: Press F11 and place the Script directly above [Main].               #
# Description: This add-on creates a bar to show which side is having a better #
#              chance of winning the battle. The winning side gets a bonus on  #
#              stats, while the losing team will have its stats decreased.     #
#==============================================================================#
#==============================================================================#
# ** Support                                                                   #
#------------------------------------------------------------------------------#
# If you require support because there is an error with this script or would   #
# be incompatible with another script, please contact the creator at the       #
# following websites:                                                          #
# - RPGMaker.org        - http://rpgmaker.org/forum/forum_posts.asp?TID=713    #
#                                                                              #
#==============================================================================#
# ** Compatibility                                                             #
#------------------------------------------------------------------------------#
# This script may be incompatible with the following scripts:                  #
# - Battle System Scripts                                                      #
#                                                                              #
# This script is tested and compatible with:                                   #
# - SDK 2.4 (Earlier versions may not be compatible)                           #
#==============================================================================#
#==============================================================================#
# ** Config Module                                                             #
#------------------------------------------------------------------------------#
#  Here you can configurate several external options.                          #
#==============================================================================#

module Morale
  
  #--------------------------------------------------------------------------
  # * Morale Variable Config
  #--------------------------------------------------------------------------
  
  # Here you can change the variable which stores the morale value.
  DefaultVariable = 5
  
  # Here you can change the starting morale at each battle. Note; you can change
  # this value at the beginning of a battle by event if needed. Just as you
  # would change a regular variable value.
  DefaultMorale = 500 # max of bar is 1000 by default
  
  #--------------------------------------------------------------------------
  # * Morale Party Stat Configuration
  #--------------------------------------------------------------------------
  
  # In this table, you can set percentages which act as a party boost
  # to the specific morale group
  
  # Morale <100
    M_G1_MAXSP = -25
    M_G1_STR = -25
    M_G1_DEX = -25
    M_G1_AGI = -25
    M_G1_INT = -25
  # Morale 100 - 300
    M_G2_MAXSP = -25
    M_G2_STR = -25
    M_G2_DEX = -25
    M_G2_AGI = -25
    M_G2_INT = -25
  # Morale 300 - 450
    M_G3_MAXSP = -10
    M_G3_STR = -10
    M_G3_DEX = -10
    M_G3_AGI = -10
    M_G3_INT = -10
  # Morale 450 - 550 (This is the middle and default starting point)
    M_G4_MAXSP = 0
    M_G4_STR = 0
    M_G4_DEX = 0
    M_G4_AGI = 0
    M_G4_INT = 0
  # Morale 550 - 700
    M_G5_MAXSP = 10
    M_G5_STR = 10
    M_G5_DEX = 10
    M_G5_AGI = 10
    M_G5_INT = 10
  # Morale 700 - 900
    M_G6_MAXSP = 25
    M_G6_STR = 25
    M_G6_DEX = 25
    M_G6_AGI = 25
    M_G6_INT = 25
  # Morale 900
    M_G7_MAXSP = 50
    M_G7_STR = 50
    M_G7_DEX = 50
    M_G7_AGI = 50
    M_G7_INT = 50
  #--------------------------------------------------------------------------
  # * Party Name Customizer
  #--------------------------------------------------------------------------

  #  you can change the names by events with the following lines:
  #  Your party:
  #  - $game_actors[1].ally = "#" Where "#" is the variable number.
  #  Enemy:
  #  - $game_actors[1].ally = "#" Where "#" is the variable number.
    
  # Here you can set a default party name as seen below.
  DefaultAlly = 1
  # Here you can set a default enemy party name as seen below.
  DefaultEnemy = 1
  
  def self.ally(id)
    case id
    #==========================================
    # Alliance Name
    # Here you can add your own names for your party
    #==========================================
    when 1 then return "Mercenaries"
    when 2 then return "Imperial Forces"
    when 3 then return "Wei Forces"
    end
    else "Mercenaries"
  end
    
  def self.enemy(id)
    case id
    #==========================================
    # Enemy Name
    # Here you can add your own names for enemy encounters
    #==========================================
    when 1 then return "Frisian Army"
    when 2 then return "Imperial Rebels"
    when 3 then return "Shu Forces"
    end
    return "Mercenaries"
  end
end

  #--------------------------------------------------------------------------
  # * NOTE! No need to go past here unless you know what to do.
  #--------------------------------------------------------------------------
#==============================================================================
# ** Game_Actor
#------------------------------------------------------------------------------
#  This window displays graphical bars and remembers the name of the parties.
#==============================================================================

class Game_Actor < Game_Battler 
  def now_exp 
    return @exp - @exp_list[@level] 
  end 
  def next_exp 
    return @exp_list[@level+1] > 0 ? @exp_list[@level+1] - @exp_list[@level] : @exp_list[@level] 
  end
end

class Window_Base
  def draw_actor_barz(actor,x, y, type, length, thick, e1, e2, c1 = Color.new(0, 0, 0, 255), c2 = Color.new(0, 0, 0, 255))
    if type == "horizontal"
      width = length
      height = thick
      self.contents.fill_rect(x, y , width + 1, height + 3, Color.new(0, 0, 0, 0))
      self.contents.fill_rect(x, y, width + 1, height + 3, Color.new(253, 53, 56, 255))
      w = width * e1.to_f / e2.to_f
      for i in 0..height
      r = c1.red + (c2.red - c1.red) * (height -i)/height + 1 * i/height
      g = c1.green + (c2.green - c1.green) * (height -i)/height + 1 * i/height
      b = c1.blue + (c2.blue - c1.blue) * (height -i)/height + 1 * i/height
      a = c1.alpha + (c2.alpha - c1.alpha)* (height -i)/height + 255 * i/height
      self.contents.fill_rect(x, y+i, w, 1, Color.new(r, g, b, a))
      end
    end
  end

  def draw_actor_battler(actor, x, y) 
    bitmap = RPG::Cache.battler(actor.battler_name, actor.battler_hue) 
    cw = bitmap.width 
    ch = bitmap.height 
    src_rect = Rect.new(0, 0, cw, ch) 
  self.contents.blt(x - cw / 2, y - ch, bitmap, src_rect) 
  end
end

class Game_Actor
  
  attr_accessor :ally_id
  attr_accessor :ally_name

  alias change_morale_ally_names setup
  def setup(actor_id)
    @ally_id = 0
    change_morale_ally_names(actor_id)
    setup_ally(Morale::DefaultAlly)
  end
  
  def setup_ally(id = 0)
    old_id = @ally_id
    @ally_id = id
    @ally_name = Morale.ally(@ally_id)
  end
  def ally=(id)
    setup_ally(id)
  end
  
end
class Game_Actor
   
  attr_accessor :enemy_id
  attr_accessor :enemy_name
    
  alias change_morale_enemy_names setup
  def setup(actor_id)
    @enemy_id = 0
    change_morale_enemy_names(actor_id)
    setup_enemy(Morale::DefaultEnemy)
  end
  
  def setup_enemy(id = 0)
    old_id = @enemy_id
    @enemy_id = id
    @enemy_name = Morale.enemy(@enemy_id)
  end
  def enemy=(id)
    setup_enemy(id)
  end
end

#==============================================================================
# ** Game_Battler (part 1)
#------------------------------------------------------------------------------
#  This class deals with battlers. It's used as a superclass for the Game_Actor
#  and Game_Enemy classes.
#==============================================================================

class Game_Battler

  #--------------------------------------------------------------------------
  # * Public Instance Variables
  #--------------------------------------------------------------------------
  attr_accessor :bonus_maxhp              # Bonus HP
  attr_accessor :bonus_maxsp              # Bonus SP
  attr_accessor :bonus_str                # Bonus Strength
  attr_accessor :bonus_dex                # Bonus Dexerity
  attr_accessor :bonus_agi                # Bonus Agility
  attr_accessor :bonus_int                # Bonus Intellect
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  
  alias morale_game_battler_extras initialize 
  
  def initialize
    morale_game_battler_extras
    @bonus_maxhp = 0
    @bonus_maxsp = 0
    @bonus_str = 0
    @bonus_dex = 0
    @bonus_agi = 0
    @bonus_int = 0
  end
  
  #--------------------------------------------------------------------------
  # * Get Maximum HP
  #--------------------------------------------------------------------------
  alias morale_game_battler_maxhp maxhp
  def maxhp
    n = morale_game_battler_maxhp
    n = [[Integer(n) + self.bonus_maxhp, 1].max, 999999].min
    return n
  end
  
  #--------------------------------------------------------------------------
  # * Get Maximum SP
  #--------------------------------------------------------------------------
  alias morale_game_battler_maxsp maxsp
  def maxsp
    n = morale_game_battler_maxsp
    n = [[Integer(n) + self.bonus_maxsp, 0].max, 9999].min
    return n
  end

  #--------------------------------------------------------------------------
  # * Get Strength (STR)
  #--------------------------------------------------------------------------
  def str
    n = [[base_str + @str_plus, 1].max, 999].min
    for i in @states
      n *= $data_states[i].str_rate / 100.0
    end
    n = [[Integer(n) + self.bonus_str, 1].max, 999].min
    return n
  end
  
  #--------------------------------------------------------------------------
  # * Get Dexterity (DEX)
  #--------------------------------------------------------------------------
  def dex
    n = [[base_dex + @dex_plus, 1].max, 999].min
    for i in @states
      n *= $data_states[i].dex_rate / 100.0
    end
    n = [[Integer(n) + self.bonus_dex, 1].max, 999].min
    return n
  end
  #--------------------------------------------------------------------------
  # * Get Agility (AGI)
  #--------------------------------------------------------------------------
  def agi
    n = [[base_agi + @agi_plus, 1].max, 999].min
    for i in @states
      n *= $data_states[i].agi_rate / 100.0
    end
    n = [[Integer(n) + self.bonus_agi, 1].max, 999].min
    return n
  end
  #--------------------------------------------------------------------------
  # * Get Intelligence (INT)
  #--------------------------------------------------------------------------
  def int
    n = [[base_int + @int_plus, 1].max, 999].min
    for i in @states
      n *= $data_states[i].int_rate / 100.0
    end
    n = [[Integer(n) + self.bonus_int, 1].max, 999].min
    return n
  end
end

#==============================================================================
# ** Window_MoraleBar
#------------------------------------------------------------------------------
#  This window displays Party & Enemy Morale
#==============================================================================

class Window_MoraleBar < Window_Base
  
  def initialize
    super(0, 0, 640, 64)
    self.contents = Bitmap.new(width - 32, height - 32)
    self.back_opacity = 160
  end
  
  def refresh 
    self.contents.clear
    self.contents.font.color = system_color
    self.contents.draw_text(270, -5 ,150, 32, "Morale")
    self.contents.font.color = normal_color
    self.contents.draw_text(80, 5, 150, 32, $game_actors[1].ally_name)
    self.contents.font.color = normal_color
    self.contents.draw_text(400, 5, 150, 32, $game_actors[1].enemy_name)
    actor = $game_party.actors[0]
    draw_actor_barz(actor, x + 220, y + 25 , "horizontal", 168, 6, $game_variables[Morale::DefaultVariable], 1000, Color.new(33, 253, 86, 255), Color.new(124, 254, 155, 255))
    if $game_variables[Morale::DefaultVariable] >= 1000
       $game_variables[Morale::DefaultVariable] = 1000
    end
  end  
  def update
    super
    if Graphics.frame_count / Graphics.frame_rate != @total_id
      refresh
    end
  end
end

#==============================================================================
# ** Scene_Battle
#------------------------------------------------------------------------------
#  This scene gives bonusses during the battles.
#==============================================================================

class Scene_Battle
  
  def give_rewards
    # Initializing and receiving party bonusses from morale
    if $game_variables[Morale::DefaultVariable] <= 100
      for actor in $game_party.actors
          actor.bonus_maxhp = 0 #Leave zero
          actor.bonus_maxsp =  (actor.maxsp / 100 * Morale::M_G1_MAXSP)
          actor.bonus_str =  (actor.str / 100 * Morale::M_G1_STR)
          actor.bonus_dex =  (actor.dex / 100 * Morale::M_G1_DEX)
          actor.bonus_agi =  (actor.agi / 100 * Morale::M_G1_AGI)
          actor.bonus_int =  (actor.int / 100 * Morale::M_G1_INT)
       end
    end    
    if $game_variables[Morale::DefaultVariable] >= 100 && $game_variables[Morale::DefaultVariable] <= 300
      for actor in $game_party.actors
          actor.bonus_maxhp = 0 #Leave zero
          actor.bonus_maxsp = (actor.maxsp / 100 * Morale::M_G2_MAXSP)
          actor.bonus_str = (actor.str / 100 * Morale::M_G2_STR)
          actor.bonus_dex = (actor.dex / 100 * Morale::M_G2_DEX)
          actor.bonus_agi = (actor.dex / 100 * Morale::M_G2_AGI)
          actor.bonus_int = (actor.dex / 100 * Morale::M_G2_INT)
       end
    end    
    if $game_variables[Morale::DefaultVariable] >= 300 && $game_variables[Morale::DefaultVariable] <= 450
      for actor in $game_party.actors
          actor.bonus_maxhp = 0 #Leave zero
          actor.bonus_maxsp = (actor.maxsp / 100 * Morale::M_G3_MAXSP)
          actor.bonus_str = (actor.str / 100 * Morale::M_G3_STR)
          actor.bonus_dex = (actor.dex / 100 * Morale::M_G3_DEX)
          actor.bonus_agi = (actor.dex / 100 * Morale::M_G3_AGI)
          actor.bonus_int = (actor.dex / 100 * Morale::M_G3_INT)
      end
    end
    if $game_variables[Morale::DefaultVariable] >= 450 && $game_variables[Morale::DefaultVariable] <= 550
      for actor in $game_party.actors
          actor.bonus_maxhp = 0 #Leave zero
          actor.bonus_maxsp = (actor.maxsp / 100 * Morale::M_G4_MAXSP)
          actor.bonus_str = (actor.str / 100 * Morale::M_G4_STR)
          actor.bonus_dex = (actor.dex / 100 * Morale::M_G4_DEX)
          actor.bonus_agi = (actor.agi / 100 * Morale::M_G4_AGI)
          actor.bonus_int = (actor.int / 100 * Morale::M_G4_INT)
       end
    end
    if $game_variables[Morale::DefaultVariable] >= 550 && $game_variables[Morale::DefaultVariable] <= 700
      for actor in $game_party.actors
          actor.bonus_maxhp = 0 #Leave zero
          actor.bonus_maxsp = (actor.maxsp / 100 * Morale::M_G5_MAXSP)
          actor.bonus_str = (actor.str / 100 * Morale::M_G5_STR)
          actor.bonus_dex = (actor.dex / 100 * Morale::M_G5_DEX)
          actor.bonus_agi = (actor.dex / 100 * Morale::M_G5_AGI)
          actor.bonus_int = (actor.dex / 100 * Morale::M_G5_INT)
       end
    end
    if $game_variables[Morale::DefaultVariable] >= 700 && $game_variables[Morale::DefaultVariable] <= 900
      for actor in $game_party.actors
          actor.bonus_maxhp = 0 #Leave zero
          actor.bonus_maxsp = (actor.maxsp / 100 * Morale::M_G6_MAXSP)
          actor.bonus_str = (actor.str / 100 * Morale::M_G6_STR)
          actor.bonus_dex = (actor.dex / 100 * Morale::M_G6_DEX)
          actor.bonus_agi = (actor.dex / 100 * Morale::M_G6_AGI)
          actor.bonus_int = (actor.dex / 100 * Morale::M_G6_INT)
       end
    end
     if $game_variables[Morale::DefaultVariable] >= 900
      for actor in $game_party.actors
          actor.bonus_maxhp = 0 #Leave zero
          actor.bonus_maxsp = (actor.maxsp / 100 * Morale::M_G7_MAXSP)
          actor.bonus_str = (actor.str / 100 * Morale::M_G7_STR)
          actor.bonus_dex = (actor.dex / 100 * Morale::M_G7_DEX)
          actor.bonus_agi = (actor.agi / 100 * Morale::M_G7_AGI)
          actor.bonus_int = (actor.int / 100 * Morale::M_G7_INT)
      end
    end
        start_phase2
    end
      
  def morale_ending
    # Initializing and Removing bonusses from morale
      for actor in $game_party.actors
      actor.bonus_maxhp = 0
      actor.bonus_maxsp = 0
      actor.bonus_str = 0
      actor.bonus_dex = 0
      actor.bonus_agi = 0
      actor.bonus_int = 0
    # Resetting default morale for next battle
       $game_variables[Morale::DefaultVariable] = Morale::DefaultMorale
    # Actually ending the Battle
       start_phase5
     end
   end
 end
 
#==============================================================================
# ** Scene_Battle (part 1)
#------------------------------------------------------------------------------
#  This class performs battle screen processing.
#==============================================================================

class Scene_Battle
  
  alias morale_battle1_update update
  
  #--------------------------------------------------------------------------
  # * Main Processing
  #--------------------------------------------------------------------------
  def main
    # Initialize each kind of temporary battle data
    $game_temp.in_battle = true
    $game_temp.battle_turn = 0
    $game_temp.battle_event_flags.clear
    $game_temp.battle_abort = false
    $game_temp.battle_main_phase = false
    $game_temp.battleback_name = $game_map.battleback_name
    $game_temp.forcing_battler = nil
    $game_variables[Morale::DefaultVariable] = Morale::DefaultMorale
    # Initialize battle event interpreter
    $game_system.battle_interpreter.setup(nil, 0)
    # Prepare troop
    @troop_id = $game_temp.battle_troop_id
    $game_troop.setup(@troop_id)
    # Make actor command window
    s1 = $data_system.words.attack
    s2 = $data_system.words.skill
    s3 = $data_system.words.guard
    s4 = $data_system.words.item
    @actor_command_window = Window_Command.new(160, [s1, s2, s3, s4])
    @actor_command_window.y = 160
    @actor_command_window.back_opacity = 160
    @actor_command_window.active = false
    @actor_command_window.visible = false
    # Make other windows
    @party_command_window = Window_PartyCommand.new
    @help_window = Window_Help.new
    @help_window.back_opacity = 160
    @help_window.visible = false
    @status_window = Window_BattleStatus.new
    @message_window = Window_Message.new
    @morale = Window_MoraleBar.new
    # Make sprite set
    @spriteset = Spriteset_Battle.new
    # Initialize wait count
    @wait_count = 0
    # Execute transition
    if $data_system.battle_transition == ""
      Graphics.transition(20)
    else
      Graphics.transition(40, "Graphics/Transitions/" +
        $data_system.battle_transition)
    end
    # Start pre-battle phase
    start_phase1
    # Main loop
    loop do
      # Update game screen
      Graphics.update
      # Update input information
      Input.update
      # Frame update
      update
      # Abort loop if screen is changed
      if $scene != self
        break
      end
    end
    # Refresh map
    $game_map.refresh
    # Prepare for transition
    Graphics.freeze
    # Dispose of windows
    @actor_command_window.dispose
    @party_command_window.dispose
    @help_window.dispose
    @status_window.dispose
    @message_window.dispose
    @morale.dispose
    if @skill_window != nil
      @skill_window.dispose
    end
    if @item_window != nil
      @item_window.dispose
    end
    if @result_window != nil
      @result_window.dispose
    end
    # Dispose of sprite set
    @spriteset.dispose
    # If switching to title screen
    if $scene.is_a?(Scene_Title)
      # Fade out screen
      Graphics.transition
      Graphics.freeze
    end
    # If switching from battle test to any screen other than game over screen
    if $BTEST and not $scene.is_a?(Scene_Gameover)
      $scene = nil
    end
  end
  #--------------------------------------------------------------------------
  # * Determine Battle Win/Loss Results
  #--------------------------------------------------------------------------
  def judge
    # If all dead determinant is true, or number of members in party is 0
    if $game_party.all_dead? or $game_party.actors.size == 0
      # If possible to lose
      if $game_temp.battle_can_lose
        # Return to BGM before battle starts
        $game_system.bgm_play($game_temp.map_bgm)
        # Battle ends
        battle_end(2)
        # Return true
        return true
      end
      # Set game over flag
      $game_temp.gameover = true
      # Return true
      return true
    end
    # Return false if even 1 enemy exists
    for enemy in $game_troop.enemies
      if enemy.exist?
        return false
      end
    end
    # Start after battle phase (win)
    morale_ending
    # Return true
    return true
  end
  
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    morale_battle1_update
    @morale.update
  end
end

#==============================================================================
# ** Scene_Battle (part 2)
#------------------------------------------------------------------------------
#  This class performs battle screen processing.
#==============================================================================

class Scene_Battle
  
  alias morale_battle2_phase1 update_phase1
  alias morale_battle2_phase2 start_phase2

  #--------------------------------------------------------------------------
  # * Frame Update (pre-battle phase)
  #--------------------------------------------------------------------------
  def update_phase1
    morale_battle2_phase1
    give_rewards
  end
  #--------------------------------------------------------------------------
  # * Start Party Command Phase
  #--------------------------------------------------------------------------
  def start_phase2
    morale_battle2_phase2
    @morale.visible = false
  end
end

#==============================================================================
# ** Scene_Battle (part 3)
#------------------------------------------------------------------------------
#  This class performs battle screen processing.
#==============================================================================

class Scene_Battle

  alias morale_battle3_phase3setup phase3_setup_command_window
  alias morale_battle3_phase3updateskill update_phase3_skill_select
  alias morale_battle3_phase3updateitem update_phase3_item_select
  alias morale_battle3_enemy_select start_enemy_select
  alias morale_battle3_enemy_select1 end_enemy_select
  alias morale_battle3_actor_select start_actor_select
  alias morale_battle3_skill_select end_skill_select
  alias morale_battle3_item_select end_item_select

  #--------------------------------------------------------------------------
  # * Go to Command Input of Previous Actor
  #--------------------------------------------------------------------------
  def phase3_prior_actor
    # Loop
    begin
      # Actor blink effect OFF
      if @active_battler != nil
        @active_battler.blink = false
      end
      # If first actor
      if @actor_index == 0
        # Start party command phase
        give_rewards
        return
      end
      # Return to actor index
      @actor_index -= 1
      @active_battler = $game_party.actors[@actor_index]
      @active_battler.blink = true
    # Once more if actor refuses command input
    end until @active_battler.inputable?
    # Set up actor command window
    phase3_setup_command_window
  end
  
  #--------------------------------------------------------------------------
  # * Actor Command Window Setup
  #--------------------------------------------------------------------------
  def phase3_setup_command_window
    @morale.visible = true
    morale_battle3_phase3setup
  end

  #--------------------------------------------------------------------------
  # * Frame Update (actor command phase : skill selection)
  #--------------------------------------------------------------------------
  def update_phase3_skill_select
    @morale.visible = false
    morale_battle3_phase3updateskill
  end
 
  #--------------------------------------------------------------------------
  # * Frame Update (actor command phase : item selection)
  #--------------------------------------------------------------------------
  def update_phase3_item_select
    @morale.visible = false
    morale_battle3_phase3updateitem
  end
  
  #--------------------------------------------------------------------------
  # * Start Enemy Selection
  #--------------------------------------------------------------------------
  def start_enemy_select
    morale_battle3_enemy_select
    @morale.visible = false
  end
  #--------------------------------------------------------------------------
  # * End Enemy Selection
  #--------------------------------------------------------------------------
  def end_enemy_select
      morale_battle3_enemy_select1
      @morale.visible = true
  end
  #--------------------------------------------------------------------------
  # * Start Actor Selection
  #--------------------------------------------------------------------------
  def start_actor_select
    morale_battle3_actor_select
    @morale.visible = false
  end
  #--------------------------------------------------------------------------
  # * End Skill Selection
  #--------------------------------------------------------------------------
  def end_skill_select
    morale_battle3_skill_select
    @morale.visible = true
  end
  #--------------------------------------------------------------------------
  # * End Item Selection
  #--------------------------------------------------------------------------
  def end_item_select
    @morale.visible = true
    morale_battle3_item_select
  end
end

#==============================================================================
# ** Scene_Battle (part 4)
#------------------------------------------------------------------------------
#  This class performs battle screen processing.
#==============================================================================

class Scene_Battle
  
  #--------------------------------------------------------------------------
  # * Start Main Phase
  #--------------------------------------------------------------------------
  
  alias morale_battle4_phase4 start_phase4
  alias morale_battle4_update4to5 update_phase4_step5
  
  def start_phase4
    morale_battle4_phase4
    @morale.visible = true
  end

  #--------------------------------------------------------------------------
  # * Frame Update (main phase step 1 : action preparation)
  #--------------------------------------------------------------------------
  def update_phase4_step1
    # Hide help window
    @help_window.visible = false
    # Determine win/loss
    if judge
      # If won, or if lost : end method
      return
    end
    # If an action forcing battler doesn't exist
    if $game_temp.forcing_battler == nil
      # Set up battle event
      setup_battle_event
      # If battle event is running
      if $game_system.battle_interpreter.running?
        return
      end
    end
    # If an action forcing battler exists
    if $game_temp.forcing_battler != nil
      # Add to head, or move
      @action_battlers.delete($game_temp.forcing_battler)
      @action_battlers.unshift($game_temp.forcing_battler)
    end
    # If no actionless battlers exist (all have performed an action)
    if @action_battlers.size == 0
      # Start party command phase
      give_rewards
      return
    end
    # Initialize animation ID and common event ID
    @animation1_id = 0
    @animation2_id = 0
    @common_event_id = 0
    # Shift from head of actionless battlers
    @active_battler = @action_battlers.shift
    # If already removed from battle
    if @active_battler.index == nil
      return
    end
    # Slip damage
    if @active_battler.hp > 0 and @active_battler.slip_damage?
      @active_battler.slip_damage_effect
      @active_battler.damage_pop = true
    end
    # Natural removal of states
    @active_battler.remove_states_auto
    # Refresh status window
    @status_window.refresh
    # Shift to step 2
    @phase4_step = 2
  end
  
  #--------------------------------------------------------------------------
  # * Make Skill Action Results
  #--------------------------------------------------------------------------
  def make_skill_action_result
    # Get skill
    @skill = $data_skills[@active_battler.current_action.skill_id]
    # If not a forcing action
    unless @active_battler.current_action.forcing
      # If unable to use due to SP running out
      unless @active_battler.skill_can_use?(@skill.id)
        # Clear battler being forced into action
        $game_temp.forcing_battler = nil
        # Shift to step 1
        @phase4_step = 1
        return
      end
    end
    # Use up SP
    @active_battler.sp -= @skill.sp_cost
    # Refresh status window
    @status_window.refresh
    @morale.visible = false
    # Show skill name on help window
    @help_window.set_text(@skill.name, 1)
    # Set animation ID
    @animation1_id = @skill.animation1_id
    @animation2_id = @skill.animation2_id
    # Set command event ID
    @common_event_id = @skill.common_event_id
    # Set target battlers
    set_target_battlers(@skill.scope)
    # Apply skill effect
    for target in @target_battlers
      target.skill_effect(@active_battler, @skill)
    end
  end

  #--------------------------------------------------------------------------
  # * Frame Update (main phase step 5 : damage display)
  #--------------------------------------------------------------------------
  def update_phase4_step5
    @morale.visible = true
    morale_battle4_update4to5
  end
end